package com.example.leetcode.trainingcamp.week1.practice2;

public class Offer16_04 {

    public String tictactoe(String[] board) {
        int len = board.length;
        boolean blank = false;
        // 这里先进行行列的检查
        for (int i =0;i<len;i++){
            int rowX = 0,colX = 0,rowO = 0,colO = 0;
            for (int j = 0;j<len;j++){
                if (board[i].charAt(j) == ' ') blank =true;
                if(board[i].charAt(j) == 'X') rowX++;
                else if(board[i].charAt(j) == 'O') rowO++;

                if (board[j].charAt(i) == 'X') colX++;
                else if (board[j].charAt(i) == 'O') {

                    colO++;

                }
            }
        //    System.out.println("rowX="+rowX);
            if (colO==len || rowO == len ) return "O";
            if (rowX==len || colX == len) return "X";
        }

        // 处理对角线
        int leftX = 0,rightX=0,rightO=0,leftO=0;
        for (int i =0;i<len;i++){
            if (board[i].charAt(i) == 'O') leftO++;
            else if(board[i].charAt(i) == 'X') leftX++;
            if (board[len-1-i].charAt(i) =='O') rightO++;
            else if (board[len-1-i].charAt(i) =='X') rightX++;
        }
        if (leftX == len || rightX==len) return "X";
        if (rightO == len || leftO == len) return "O";

        return blank ? "Pending" : "Draw";
    }

}

/**
 * //   " O    X"
 *      " O     "
 *      "     O "
 *      "XXXXXXX"
 *      "  O    "
 *      " O   O "
 *      "O  X OO"
 */
class DemoOffer16_04{
    public static void main(String[] args) {
        String[] board = {
                " O    X"," O     ","     O ","XXXXXXX","  O    "," O   O ","O  X OO"};
        Offer16_04 o = new Offer16_04();
        String s =o.tictactoe(board);
        System.out.println(s);
    }
}

